package com.Practice.estore.model;
import java.util.Objects;
import java.io.Serializable;

/**
 * 商品实体类
 */
public class Product implements Comparable<Product>, Serializable {

    // 添加序列化版本UID
    private static final long serialVersionUID = 1L;

    private String id;      // 商品ID
    private String name;    // 商品名称
    private double price;   // 价格
    private int stock;      // 库存

    public Product(String id, String name, double price, int stock) {
        this.id = id;
        this.name = name;
        this.price = price;
        this.stock = stock;
    }

    // Getters and Setters
    public String getId() { return id; }
    @SuppressWarnings("unused")
    public void setId(String id) { this.id = id; }
    public String getName() { return name; }
    @SuppressWarnings("unused")
    public void setName(String name) { this.name = name; }
    public double getPrice() { return price; }
    @SuppressWarnings("unused")
    public void setPrice(double price) { this.price = price; }
    public int getStock() { return stock; }
    @SuppressWarnings("unused")
    public void setStock(int stock) { this.stock = stock; }

    @Override
    public String toString() {
        return String.format("ID: %-5s | 名称: %-20s | 价格: ¥%-8.2f | 库存: %d", id, name, price, stock);
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Product product = (Product) o;
        return Objects.equals(id, product.id);
    }

    @Override
    public int hashCode() {
        return Objects.hash(id);
    }

    @Override
    public int compareTo(Product other) {
        if (other == null) {
            return 1; // 当前对象排在前面
        }
        // 按商品ID的字母顺序升序排列
        return this.id.compareTo(other.id);
    }
}